package com.wy.xinzhi.common.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.dao.IDataBaseDao;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.AbstractBaseService;
import com.wy.xinzhi.common.pojos.TCustomerCheck;
import com.wy.xinzhi.common.pojos.THoliday;

/**
 * 健身预约
 * 
 * @author X
 * 
 */
public class CustomerCheckService extends AbstractBaseService {
	private static Logger logger = LoggerFactory
			.getLogger(CustomerCheckService.class);
	static PropertiesUtil p = new PropertiesUtil();

	private JdbcTemplate jdbcTemplate;
	private HibernateDaoSupport hibernateDao;
	private IDataBaseDao dataBaseDao;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public HibernateDaoSupport getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(HibernateDaoSupport hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IDataBaseDao getDataBaseDao() {
		return dataBaseDao;
	}

	public void setDataBaseDao(IDataBaseDao dataBaseDao) {
		this.dataBaseDao = dataBaseDao;
	}

	/**
	 * 获取每日预约时间段
	 * 
	 * @param checkDate
	 * @return
	 */
	public List<Map<String, Object>> queryReservePeriods(String checkDate,
			String storeId) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		StringBuffer sql = new StringBuffer("select t1.*,(CASE WHEN t1.max_no<=t2.CT THEN '0' else '1' END) AS isIdel,");
		sql.append("(CASE WHEN t3.holi_id is null THEN '0' else '1' END) AS isHol ");
		sql.append("from t_reserve_period t1 LEFT JOIN ");
		sql.append("(select cc.store_id,cc.period_id,cc.check_date,count(*) as CT from t_customer_check cc inner join t_reserve_period rp ON cc.period_id = rp.period_id ");
		sql.append("WHERE cc.check_date='");
		sql.append(checkDate);
		sql.append("' and cc.store_id=");
		sql.append(storeId);
		sql.append(" and cc.check_status !='2' group by cc.store_id,cc.period_id,cc.check_date) t2 ");
		sql.append("ON t1.period_id = t2.period_id ");
		sql.append("LEFT JOIN t_holiday t3 ON t2.check_date = t3.holi_Date ");
		list = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("查询" + checkDate + "门店" + storeId + "的预约时间段，详细："
				+ sql.toString());
		return list;
	}

	/**
	 * 查询用户预约信息
	 * 
	 * @param CustomerIds
	 * @return
	 */
	public List<Map<String, Object>> queryCustomerChecks(String CustomerIds) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		StringBuffer sql = new StringBuffer(
				"select * from t_customer_check where customer_id= '");
		sql.append(CustomerIds);
		sql.append("' order by check_date desc");
		list = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("查询用户" + CustomerIds + "的预约信息，详细：" + sql.toString());
		return list;
	}

	/**
	 * 检查选定时间段是否空闲
	 * 
	 * @param checkDate
	 * @param periodId
	 * @param storeId
	 * @return
	 */
	public boolean getIsIdle(String checkDate, String periodId, String storeId) {
		boolean isIdle = true;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		StringBuffer sql = new StringBuffer(
				"select * from t_reserve_period t1 INNER JOIN (select cc.store_id,cc.period_id,cc.check_date,count(*) as CT from t_customer_check cc inner join t_reserve_period rp ON cc.period_id = rp.period_id WHERE cc.check_date='");
		sql.append(checkDate);
		sql.append("' and cc.store_id= ");
		sql.append(storeId);
		sql.append("and cc.period_id =");
		sql.append(periodId);
		sql.append("and cc.check_status !='2' group by cc.store_id,cc.period_id,cc.check_date) t2 ON t1.period_id = t2.period_id WHERE t2.CT >= t1.max_no");
		list = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		if (list != null && list.size() > 0) {
			isIdle = false;
		}
		logger.debug("查询" + checkDate + "门店" + storeId + "在预约时间段" + periodId
				+ "是否可预约，详细：" + sql.toString());
		return isIdle;
	}

	/**
	 * 删除用户预约信息
	 * 
	 * @param obj
	 * @return
	 * @throws ServiceException
	 */
	public boolean deleteCustomerCheck(TCustomerCheck obj)
			throws ServiceException {
		boolean isSucc = true;
		try {
			hibernateDao.getHibernateTemplate().delete(obj);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			isSucc = false;
			throw new ServiceException(e);
		} finally {
			return isSucc;
		}
	}

	/**
	 * 新增预约
	 * 
	 * @param reservePeriod
	 */
	public void addCustomerCheck(TCustomerCheck customerCheck)
			throws ServiceException {
		try {
			hibernateDao.getHibernateTemplate().save(customerCheck);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			throw new ServiceException(e);
		}
	}

	/**
	 * 修改预约
	 * 
	 * @param customerCheck
	 * @throws ServiceException
	 */
	public void updateCustomerCheck(TCustomerCheck customerCheck)
			throws ServiceException {
		try {
			hibernateDao.getHibernateTemplate().update(customerCheck);
		} catch (DataAccessException e) {
			logger.error("系统错误:{}", e);
			throw new ServiceException(e);
		}
	}
}
